package com.wajahatkarim3.rdbexplorer.demo.slice;

import com.wajahatkarim3.rdbexplorer.demo.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class TodoItemProvider extends BaseItemProvider {

    public interface ItemClickedListener {
        void onChecked(int i);
    }

    private int i;

    private Context context;

    private ItemClickedListener itemClickedListener;

    private List<Todo> list;

    public TodoItemProvider(Context context, List<Todo> list) {
        this.context = context;
        this.list = list;
    }

    public ItemClickedListener getItemClickedListener() {
        return itemClickedListener;
    }

    public void setItemClickedListener(ItemClickedListener itemClickedListener) {
        this.itemClickedListener = itemClickedListener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        ProviderHolder holder;
        Component cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_todo_item_layout,null,false);
        holder = new ProviderHolder(cpt, i);
        component = cpt;

        holder.bindData(list.get(i));
        component.setTag(holder);

        Button delBtn = (Button) cpt.findComponentById(ResourceTable.Id_checkDone);

        if (delBtn != null) {
            delBtn.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    itemClickedListener.onChecked(i);
                }
            });
        }

        return component;
    }

    private class ProviderHolder {
        Component component;
        Text txtTitle;
        Text txtId;

        public ProviderHolder(Component cpt, int i) {
            component = cpt;
            txtTitle = (Text) cpt.findComponentById(ResourceTable.Id_txtTodoTitle);
            txtId = (Text) cpt.findComponentById(ResourceTable.Id_txtTodoId);
        }

        public void bindData(Todo data) {
            txtId.setText(data.getId());
            txtTitle.setText(data.getTitle());
        }
    }

}
